
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//


using DotNetNuke.Entities.Modules;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;

namespace DotNetNuke.Modules.Html
{

	/// -----------------------------------------------------------------------------
	/// <summary>
	/// The Settings ModuleSettingsBase is used to manage the 
	/// settings for the HTML Module
	/// </summary>
	/// <remarks>
	/// </remarks>
	/// <history>
	/// 	[leupold]	    08/12/2007	created
	/// </history>
	/// -----------------------------------------------------------------------------
	public partial class Settings : DotNetNuke.Entities.Modules.ModuleSettingsBase
	{

		#region "Protected Methods"
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			cboWorkflow.SelectedIndexChanged += new EventHandler(cboWorkflow_SelectedIndexChanged);
		}
		#endregion
		protected void cboWorkflow_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            DisplayWorkflowDetails();
		}

        private void DisplayWorkflowDetails()
		{
			if ((cboWorkflow.SelectedValue != null)) {
				WorkflowStateController objWorkflow = new WorkflowStateController();
				string strDescription = "";
				ArrayList arrStates = objWorkflow.GetWorkflowStates(int.Parse(cboWorkflow.SelectedValue));
				if (arrStates.Count > 0) {
					foreach (WorkflowStateInfo objState in arrStates) {
						strDescription = strDescription + " >> " + "<span class=\"NormalBold\">" + objState.StateName + "</span>";
					}
					strDescription = strDescription + "<br />" + ((WorkflowStateInfo)arrStates[0]).Description;
				}
				lblDescription.Text = strDescription;
			}
		}

		#region "Base Method Implementations"

		/// -----------------------------------------------------------------------------
		/// <summary>
		/// LoadSettings loads the settings from the Database and displays them
		/// </summary>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// </history>
		/// -----------------------------------------------------------------------------
		public override void LoadSettings()
		{
			try {
				if (!Page.IsPostBack) {
                    HtmlTextController _htmlTextController = new HtmlTextController();
                    WorkflowStateController _workflowStateController = new WorkflowStateController();

					// get replace token settings
					if (ModuleSettings["HtmlText_ReplaceTokens"] != null) 
                    {
						chkReplaceTokens.Checked = Convert.ToBoolean(ModuleSettings["HtmlText_ReplaceTokens"]);
					}

					// get workflow/version settings
					ArrayList arrWorkflows = new ArrayList();
                    foreach (WorkflowStateInfo objState in _workflowStateController.GetWorkflows(PortalId))
                    {
						if (!objState.IsDeleted) {
							arrWorkflows.Add(objState);
						}
					}
					cboWorkflow.DataSource = arrWorkflows;
					cboWorkflow.DataBind();
                    KeyValuePair<string, int> workflow = _htmlTextController.GetWorkflow(ModuleId, TabId, PortalId);
					if ((cboWorkflow.Items.FindByValue(workflow.Value.ToString()) != null)) {
						cboWorkflow.Items.FindByValue(workflow.Value.ToString()).Selected = true;
					}
					DisplayWorkflowDetails();


                    if (rblApplyTo.Items.FindByValue(workflow.Key) != null)
					{
						rblApplyTo.Items.FindByValue(workflow.Key).Selected = true;
                    }

				}
			//Module failed to load
			} catch (Exception exc) {
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		/// -----------------------------------------------------------------------------
		/// <summary>
		/// UpdateSettings saves the modified settings to the Database
		/// </summary>
		/// -----------------------------------------------------------------------------
		public override void UpdateSettings()
		{

			try {
                HtmlTextController _htmlTextController = new HtmlTextController();
				WorkflowStateController objWorkflow = new WorkflowStateController();

				// update replace token setting
				DotNetNuke.Entities.Modules.ModuleController objModules = new DotNetNuke.Entities.Modules.ModuleController();
				objModules.UpdateModuleSetting(ModuleId, "HtmlText_ReplaceTokens", chkReplaceTokens.Checked.ToString());

				// disable module caching if token replace is enabled
				if (chkReplaceTokens.Checked) {
					DotNetNuke.Entities.Modules.ModuleInfo objModule = objModules.GetModule(ModuleId, TabId, false);
					if (objModule.CacheTime > 0) {
						objModule.CacheTime = 0;
						objModules.UpdateModule(objModule);
					}
				}

				// update workflow/version settings
               switch(rblApplyTo.SelectedValue)
			   {
				   case "Module":
                       _htmlTextController.UpdateWorkflow(ModuleId, rblApplyTo.SelectedValue, Int32.Parse(cboWorkflow.SelectedValue), chkReplace.Checked);
						break;
				   case "Page":
                        _htmlTextController.UpdateWorkflow(TabId, rblApplyTo.SelectedValue, Int32.Parse(cboWorkflow.SelectedValue), chkReplace.Checked);
						break;
				   case "Site":
                        _htmlTextController.UpdateWorkflow(PortalId, rblApplyTo.SelectedValue, Int32.Parse(cboWorkflow.SelectedValue), chkReplace.Checked);
						break;
			}

			//Module failed to load
			} catch (Exception exc) {
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		#endregion

	}

}


